package android.slc.user.ui.tree

import android.os.Bundle
import android.slc.adapter.CommonlyRecycler
import android.slc.appbase.ui.fragment.base.AppMvvmBaseFragment
import android.slc.extras.component.tree.entity.SlcTreeNav
import android.slc.extras.component.tree.ui.TreeNavVm
import android.slc.user.R
import android.slc.user.databinding.UserFragmentTreeListNavBinding
import android.view.View
import android.widget.LinearLayout
import androidx.databinding.Observable
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.chad.library.adapter.base.BaseQuickAdapter

/**
 *
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 3:24
 *
 */
open class UserTreeListNavFragment :
    AppMvvmBaseFragment<UserFragmentTreeListNavBinding, TreeNavVm>() {

    private lateinit var swipeRecycler: CommonlyRecycler<SlcTreeNav>

    override fun onInitViewModel() {
        viewModel = getVmByActivityProvider(TreeNavVm::class.java)
    }

    override fun setContentView(): Any {
        return R.layout.user_fragment_tree_list_nav
    }

    override fun onBindView(savedInstanceState: Bundle?) {
        super.onBindView(savedInstanceState)
        swipeRecycler = CommonlyRecycler.CommonlyRecyclerBuilder<SlcTreeNav>(contentView)
            .setRecyclerViewId(R.id.rv_nav)
            .setLayoutManager(object :
                LinearLayoutManager(context, RecyclerView.HORIZONTAL, false) {
                override fun canScrollVertically(): Boolean {
                    return super.canScrollVertically() && swipeRecycler.isIsAllowTouch
                }

                override fun canScrollHorizontally(): Boolean {
                    return super.canScrollHorizontally() && swipeRecycler.isIsAllowTouch
                }
            })
            .build {
                val userTreeNavAdapter =
                    UserTreeNavAdapter(viewModel.slcTreeNavList)
                userTreeNavAdapter.setHeaderView(
                    layoutInflater.inflate(
                        R.layout.user_item_tree_nav_head,
                        null
                    ), orientation = LinearLayout.HORIZONTAL
                )
                userTreeNavAdapter.setOnItemClickListener { _: BaseQuickAdapter<*, *>, _: View, position: Int ->
                    viewModel.pop(position)
                }
                userTreeNavAdapter
            }
        swipeRecycler.refresh()

    }

    override fun onRegisterLiveEvent() {
        super.onRegisterLiveEvent()
        viewModel.getSlcTreeNavListOf()
            .addOnPropertyChangedCallback(object : Observable.OnPropertyChangedCallback() {
                override fun onPropertyChanged(sender: Observable, propertyId: Int) {
                    swipeRecycler.refresh()
                }
            })
    }
}